package com.rk.study02;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 *  输入数据
 *  7 	13560436666	  120.196.100.99		1116		 954			200
 *  id   手机号码		  网络ip			        上行流量      下行流量        网络状态码
 *
 *  输出数据
 *  13560436666 		1116		      954 			2070
 *  手机号码		        上行流量           下行流量	    总流量
 */
public class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    Text k = new Text();
    FlowBean v = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 1.获取一行
        String line = value.toString();

        // 2.切割字符
        String[] fields = line.split("\t");

        // 3.封装
        // 取出手机号
        String phone = fields[1];

        // 取出上行流量
        String upFlow = fields[fields.length - 3];

        // 取出下行流量
        String downFlow = fields[fields.length - 2];

        k.set(phone);
        v.setUpFlow(Long.parseLong(upFlow));
        v.setDownFlow(Long.parseLong(downFlow));

        // 4.写出
        context.write(k, v);
    }
}
